<?php
include '../class/class.usulan.php';
include '../class/class.detail.php';
include '../class/class.pegawai.php';
include '../class/class.refcuti.php';
include '../class/class.tujuan.php';
include '../class/class.golongan.php';
include '../class/class.jabatan.php';
include '../class/class.level.php';
include '../class/class.kanwil.php';

if (isset($_REQUEST['aksi_proses_usulan'])) {
    $id_det_usulan = $_REQUEST['id_det_usulan'];


    $detailDao = new DetailDao();

    $detail = $detailDao->getDetailById($id_det_usulan);

    $tgl_mulai = $detail['tgl_mulai'];
    $tgl_selesai = $detail['tgl_selesai'];
    $id_cuti = $detail['id_cuti'];
    $id_tujuan = $detail['id_tujuan'];
    $pegawaiDao = new PegawaiDao();
    $nip = $detail['nip'];
    $id_tanggapan=$detail['id_tanggapan'];
    $keterangan=$detail['keterangan'];
    $nama_dokumen=$detail['nama_dokumen'];
    $pegawai = $pegawaiDao->getPegawai($nip);

    $msg = true;
    $nama = $pegawai['nama'];
    $kdgol = $pegawai['gol'];
    $unit = $pegawai['unit'];
    $eselon = $pegawai['eselon'];

    $cuti = new RefCutiDao();
    $tujuan = new TujuanDao();
    $golDao = new GolonganDao();
    $jabatanDao=new JabatanDao();
    $gol=$golDao->getGolongan($kdgol);
    $nama_gol=$gol['nmgol'];
    $pangkat=$gol['pangkat'];
    $data = array(
        'msg' => $msg,
        'tgl_mulai' => $tgl_mulai,
        'tgl_selesai' => $tgl_selesai,
        'id_cuti' => $cuti->getCuti($id_cuti),
        'id_tujuan' => $tujuan->getTujuan($id_tujuan),
        'nip' => $nip,
        'nama' => $nama,
        'golongan' => $nama_gol,
        'pangkat' => $pangkat,
        'unit' => $unit,
        'eselon' => $jabatanDao->getJabatan($nip),
        'id_tanggapan' => $id_tanggapan,
        'keterangan' => $keterangan,
        'nama_dokumen' => $nama_dokumen,
    );

    echo json_encode($data);
}

if(isset($_REQUEST['aksi_daftar_pegawai'])){
    $msg = false;
    $daftar=array();
    
    $cuti=array();
    $tujuan=array();

    $usulanDao = new UsulanDao();
    $pegawaiDao = array(); 
    $levelDao=array();
    
    $pegawai = array();

    $list_peg = $usulanDao->getAllUsulanPeg();
    if ($list_peg != false) {
        $msg = true;
        $i=0;
        foreach($list_peg as $row){
            $cuti[$i]=new RefCutiDao();
            $tujuan[$i]= new TujuanDao();
            $pegawaiDao[$i]=new PegawaiDao();
            $pegawai[$i] = $pegawaiDao[$i]->getPegawai($row['nip']);
            $levelDao[$i]= new LevelDao();
            $daftar[$i]=array(
                'id_det_usulan' => $row['id_det_usulan'],
                'id_usulan' => $row['id_usulan'],
                'nama_peg' => $pegawai[$i]['nama'],
                'nip' => $row['nip'],
                'tgl_mulai' => $row['tgl_mulai'],
                'tgl_selesai' => $row['tgl_selesai'],
                'id_cuti' => $cuti[$i]->getCuti($row['id_cuti']),
                'id_tujuan' => $tujuan[$i]->getTujuan($row['id_tujuan']),
                'level' => $levelDao[$i]->getLevel($row['id_level']),
                'stat' => $row['id_level'],
            );
            $i++;
        }        
    }

    $data = array(
        'msg' => $msg,
        'daftar' => $daftar,
    );

    echo json_encode($data);
}

if(isset($_REQUEST['aksi_daftar_pegawai_filter'])){
    
    $jen_dok=$_REQUEST['jen_dok'];

    $msg = false;
    $daftar=array();
    
    $cuti=array();
    $tujuan=array();

    $usulanDao = new UsulanDao();
    $pegawaiDao = array(); 
    $levelDao=array();
    
    $pegawai = array();

    $list_peg = $usulanDao->getAllUsulanPegFilter($jen_dok);
    if ($list_peg != false) {
        $msg = true;
        $i=0;
        foreach($list_peg as $row){
            $cuti[$i]=new RefCutiDao();
            $tujuan[$i]= new TujuanDao();
            $pegawaiDao[$i]=new PegawaiDao();
            $pegawai[$i] = $pegawaiDao[$i]->getPegawai($row['nip']);
            $levelDao[$i]= new LevelDao();
            $daftar[$i]=array(
                'id_det_usulan' => $row['id_det_usulan'],
                'id_usulan' => $row['id_usulan'],
                'nama_peg' => $pegawai[$i]['nama'],
                'nip' => $row['nip'],
                'tgl_mulai' => $row['tgl_mulai'],
                'tgl_selesai' => $row['tgl_selesai'],
                'id_cuti' => $cuti[$i]->getCuti($row['id_cuti']),
                'id_tujuan' => $tujuan[$i]->getTujuan($row['id_tujuan']),
                'level' => $levelDao[$i]->getLevel($row['id_level']),
            );
            $i++;
        }        
    }

    $data = array(
        'msg' => $msg,
        'daftar' => $daftar,
    );

    echo json_encode($data);
}



if (isset($_REQUEST['list_usulan'])) {

   

    $usulanDao = new usulanDao();

    $usulan = $usulanDao->getAllUsulan();
    $kanwil = array();

    $list = array();
    $jml_peg = array();
    $data = array();
    $i = 0;
    if($usulan!=false){
        foreach ($usulan as $row) {
            $list[$i] = new usulanDao();
            //$kanwil[$i] = new KanwilDao();


            $jml = $list[$i]->getJmlPegawai($row['id_usulan']);
            if (!$jml) {
                $jml_peg[$i] = 'Belum Di Entry';
            } else {
                $jml_peg[$i] = $jml;
            }
            $data[$i] = array(
                'id_usulan' => $row['id_usulan'],
                'no_surat' => $row['no_surat'],
                'tgl_surat' => $row['tgl_surat'],
                'level' => $row['level_usulan'],
                //'kanwil' => $kanwil[$i]->getNmKanwil($row['kdkanwil']),
                'jml' => $jml_peg[$i],
            );
            $i++;
        }
    }

    echo json_encode($data);
}

if(isset($_REQUEST['aksi_proses_peg'])){
    $id_proses=$_POST['id_aksi'];
    $det=$_POST['details'];

    $details=explode(',',$det);
    $detail=array();
    $detailDao=array();

    $msg=array();
    $error='Proses Gagal dilakukan ';
    $hasil=array();
    $pegawai=array();
    $nip=array();
    $pegawaiDao=array();
    $nama=array();
    $pes=false;

    for($i=0;$i<count($details);$i++){
        $detail[$i]=new Detail();
        $detail[$i]->id_detail=$details[$i];
        $detail[$i]->id_level=$id_proses;
        $detailDao[$i]=new DetailDao();
        $hasil[$i]=$detailDao[$i]->prosesDetailPegawai($detail[$i]);
        if($hasil[$i]){
            $msg[$i]=true;
        }else{
            $msg[$i]=false;
            $pegawai[$i]=$detailDao[$i]->getgetDetailById($detail[$i]);
            $nip[$i]=$pegawai[$i]['nip'];
            $pegawaiDao[$i]=new PegawaiDao();
            $res=$pegawaiDao[$i]->getPegawai($nip[$i]);
            $nama[$i]=$res['nama'];
        }
    }

    if(!in_array(false, $msg)){
        $pes=true;
    }else{
        $error='Gagal Proses untuk pegawai ';
        for($i=0;$i<count($nama);$i++){
            $error.=" ".$nama[$i];
            if($i!=(count($nama)-1)){
                $error.=", ";
            } 
        }
    }
    $data=array(
        'msg' => $pes,
        'error' => $error,
        );
    echo json_encode($data);
}

if(isset($_REQUEST['aksi_setujui_usulan'])){
    $id_proses=$_POST['id_aksi'];
    $det=$_POST['usulans'];

    $usulans=explode(',',$det);
    $detailDao=array();
    $usulanDao=array();
    $usulan=array();
    $nomer=array();

    $msg=array();
    $error='Proses Gagal dilakukan ';
    $hasil=array();
    $pes=false;

    for($i=0;$i<count($usulans);$i++){
        $detailDao[$i]=new DetailDao();
        $hasil[$i]=$detailDao[$i]->prosesDetailPegawaiByUsulan($usulans[$i],$id_proses);
        $usulanDao[$i]=new UsulanDao();
        if($hasil[$i]){
            $usulanDao[$i]->terimaUsulan($usulans[$i]);
            $msg[$i]=true;
        }else{
            $msg[$i]=false;
            $usulan[$i]=new Usulan();
            $usulan[$i]->id_usulan=$usulans[$i];
            $res=$usulanDao[$i]->getById($usulan[$i]);
            $nomer[$i]=$res['no_surat'];
        }
    }

    if(!in_array(false, $msg)){
        $pes=true;
    }else{
        $error='Gagal Proses untuk usulan ';
        for($i=0;$i<count($nomer);$i++){
            $error.=" nomer surat ".$nomer[$i];
            if($i!=(count($nomer)-1)){
                $error.=", ";
            } 
        }
    }
    $data=array(
        'msg' => $pes,
        'error' => $error,
        );
    echo json_encode($data);
}

if(isset($_REQUEST['aksi_get_all_level'])){

        $levelDao=new LevelDao();

        $level=$levelDao->getAllLevelUsulan();

        echo json_encode($level);

    }
    if(isset($_REQUEST['aksi_get_all_level2'])){

        $levelDao=new LevelDao();

        $level=$levelDao->getAllLevel();

        echo json_encode($level);

    }
?>
